/**
 * @description router全局配置，如有必要可分文件抽离，其中asyncRoutes只有在intelligence模式下才会用到，pro版只支持remixIcon图标，具体配置请查看vip群文档
 */
import type { App } from 'vue'
import type { RouteRecordRaw } from 'vue-router'
import { createRouter, createWebHashHistory, createWebHistory, RouteRecordName } from 'vue-router'
import type { VabRouteRecordRaw } from './types'
import { authentication, base, isHashRouterMode } from '/@/config'
import { setupPermissions } from '/@/router/permissions'
import Layout from '/@vab/layouts/index.vue'

export const constantRoutes: VabRouteRecordRaw[] = [
  {
    path: '/login',
    name: 'Login',
    component: () => import('/@/views/login/index.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/register',
    name: 'Register',
    component: () => import('/@/views/register/index.vue'),
    meta: {
      hidden: true,
    },
  },

  {
    path: '/403',
    name: '403',
    component: () => import('/@/views/error/403.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/404',
    name: '404',
    component: () => import('/@/views/error/404.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/500',
    name: 'Error500',
    component: () => import('/@/views/error/500.vue'),
    meta: {
      hidden: true,
    },
  },
  {
    path: '/503',
    name: 'Error503',
    component: () => import('/@/views/error/503.vue'),
    meta: {
      hidden: true,
    },
  },
]

export const asyncRoutes: VabRouteRecordRaw[] = [
  {
    path: '/',
    name: 'Root',
    component: Layout,
    meta: {
      title: '首页',
      icon: 'home-2-line',
    },
    children: [
      {
        path: '',
        name: 'Index',
        component: () => import('/@/views/index/index.vue'),
        meta: {
          title: '人员列表',
          icon: 'user-3-line',
          noClosable: true,
        },
      },
    ],
  },

  {
    path: '/authManagement',
    name: 'AuthManagement',
    component: Layout,
    meta: {
      title: '权限',
      icon: 'user-settings-line',
    },
    children: [
      {
        path: 'user',
        name: 'User',
        component: () => import('/@/views/auth/user/index.vue'),
        meta: {
          title: '用户配置',
          icon: 'user-3-line',
        },
      },
      {
        path: 'role',
        name: 'Role',
        component: () => import('/@/views/auth/role/index.vue'),
        meta: {
          title: '角色管理',
          icon: 'pass-valid-line',
        },
      },
      {
        path: 'menu',
        name: 'Menu',
        component: () => import('/@/views/auth/menu/index.vue'),
        meta: {
          title: '菜单管理',
          icon: 'mist-line',
        },
      },
    ],
  },
  {
    path: '/:pathMatch(.*)*',
    redirect: '/404',
    name: 'NotFound',
    meta: {
      title: '404',
      hidden: true,
    },
  },
]

const router = createRouter({
  history: isHashRouterMode ? createWebHashHistory(base) : createWebHistory(base),
  routes: constantRoutes as RouteRecordRaw[],
})

function fatteningRoutes(routes: VabRouteRecordRaw[]): VabRouteRecordRaw[] {
  return routes.flatMap((route) => {
    return route.children ? fatteningRoutes(route.children) : route
  })
}

function addRouter(routes: VabRouteRecordRaw[]) {
  routes.forEach((route: VabRouteRecordRaw) => {
    if (!router.hasRoute(route.name)) router.addRoute(route as RouteRecordRaw)
    if (route.children) addRouter(route.children)
  })
}

export function resetRouter(routes: VabRouteRecordRaw[] = constantRoutes) {
  routes.map((route: VabRouteRecordRaw) => {
    if (route.children) {
      route.children = fatteningRoutes(route.children)
    }
  })
  router.getRoutes().forEach((route) => {
    if (route.name) {
      const routeName: RouteRecordName = route.name
      router.hasRoute(routeName) && router.removeRoute(routeName)
    }
  })
  addRouter(routes)
}

// export function getModuleRouter() {
//   const modules = import.meta.glob('./modules/**/*.ts', { eager: true })
//   const moduleRouter: any = []
//   Object.keys(modules).forEach((key) => {
//     const mod = (modules as any)[key].default || {}
//     const modList = Array.isArray(mod) ? [...mod] : [mod]
//     moduleRouter.push(...modList)
//   })
//   asyncRoutes.push(...moduleRouter)
// }

export const setupRouter = (app: App<Element>) => {
  if (authentication === 'intelligence') addRouter(asyncRoutes)
  setupPermissions(router)
  app.use(router)
  return router
}

export default router
